using System;
using System.Configuration;
using System.Data;
using digit.Core;


namespace SMS.ServiceManager
{
    public class ServicesManager
    {
        #region CONSTRUCTORS
        static ServicesManager()
        {
            _data = new ServicesData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static ServicesData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(string serviceName, string serviceBackup, byte serviceIndex, string serviceNumber, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = serviceName;
                values[1] = serviceBackup;
                values[2] = serviceIndex;
                values[3] = serviceNumber;
                values[4] = status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(ServicesInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = info.ServiceName;
                values[1] = info.ServiceBackup;
                values[2] = info.ServiceIndex;
                values[3] = info.ServiceNumber;
                values[4] = info.Status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long serviceID, string serviceName, string serviceBackup, byte serviceIndex, string serviceNumber, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = serviceID;
                values[1] = serviceName;
                values[2] = serviceBackup;
                values[3] = serviceIndex;
                values[4] = serviceNumber;
                values[5] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(ServicesInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = info.ServiceID;
                values[1] = info.ServiceName;
                values[2] = info.ServiceBackup;
                values[3] = info.ServiceIndex;
                values[4] = info.ServiceNumber;
                values[5] = info.Status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long serviceID)
        {
            _error = "";
            try
            {
                object[] values = { serviceID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(ServicesInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.ServiceID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static ServicesInfo Select(long serviceID)
        {
            _error = "";
            try
            {
                object[] values = { serviceID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                ServicesInfo info = new ServicesInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable ListByRange(string range)
        {
            _error = "";
            try
            {
                return _data.ListByRange(range);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        #endregion

    }
}